﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using RemoteObject;
using System.Threading;

namespace RemoteClient.UI
{
    public partial class ClientForm : Form
    {
        public ClientForm()
        {
            InitializeComponent();
            remoteFileListView1.ParentCForm = this;
        }

        private void distributeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartDistribute();
        }

        private void downloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartNewDownload();
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            startJobs();
        }

        private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stopJobs();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dispose();
            Application.Exit();
        }

        private void distributeSBtn_Click(object sender, EventArgs e)
        {
            StartDistribute();
        }

        private void uploadSBtn_Click(object sender, EventArgs e)
        {
            StartNewUpload();
        }        

        private void downloadSBtn_Click(object sender, EventArgs e)
        {
            StartNewDownload();
        }

        private void startSBtn_Click(object sender, EventArgs e)
        {
            startJobs();
        }

        private void puaseSBtn_Click(object sender, EventArgs e)
        {
            stopJobs();
        }

        private void startJobs()
        {
            if (tabControl1.SelectedTab == uploadPage)
            {
                uJobListView1.StartUpload();
            }
            else if (tabControl1.SelectedTab == downloadPage)
            {
                downloadPanel1.StartDownload();
            }
        }

        private void stopJobs()
        {
            if (tabControl1.SelectedTab == uploadPage)
            {
                uJobListView1.StopUpload();
            }
            else if (tabControl1.SelectedTab == downloadPage)
            {
                downloadPanel1.StopDownload();
            }
        }        

        public void StartDistribute()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Multiselect = true;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                for (int i = 0; i < openDialog.FileNames.Length; i++)
                { 
                    String fnLocal = openDialog.FileNames[i];
                    string fnCloud = String.Concat(Common.CloudPath,openDialog.SafeFileNames[i]);
                    DistributeRequestor requstor = new DistributeRequestor(fnCloud, fnLocal, "10001");
                    Thread thread = new Thread(new ThreadStart(requstor.Run));
                    thread.Start();
                }                
            }
        }
        
        public void StartNewUpload()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Multiselect = true;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                for (int i = 0; i < openDialog.FileNames.Length; i++)
                {
                    String fnLocal = openDialog.FileNames[i];
                    string fnCloud = String.Concat(Common.CloudPath, openDialog.SafeFileNames[i]);
                    UploadRequestor requstor = new UploadRequestor(fnCloud, fnLocal, "10001");
                    Thread thread = new Thread(new ThreadStart(requstor.StartUpload));
                    thread.Start();
                    uJobListView1.AddJob(requstor);
                }
            }
        }

        public void StartNewDownload()
        {
            foreach (DownloadJob job in remoteFileListView1.GetSelectedJobs())
            {
                StartDownload(job);
            }
        }                    

        private void StartDownload(RemoteObject.DownloadJob dJob)
        {
            DownloadJob downloadJob = new DownloadJob(String.Concat(Common.LocalPath, dJob.GetJobName()), String.Concat(Common.CloudPath, dJob.GetJobName()), Common.CloudServerURL);
            Thread thread = new Thread(new ThreadStart(downloadJob.Start));
            thread.Start();

            downloadPanel1.AddJob(downloadJob);
        }        
    }
}
